//
//  PKProductMainViewController.m
//  PianKe
//
//  Created by mingtao on 15/9/25.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKProductMainViewController.h"
#import "PKproductMainListTableViewCell.h"
#import "PKRequestManager.h"

@interface PKProductMainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *mainTableView;//主页面

@property(nonatomic,strong)NSMutableArray *dataSuorceArray;//提供数据的数组

@property(nonatomic,assign)BOOL isloading;//是否在刷新

@property(nonatomic,strong)UILabel *titlelabel;//显示标题的lable

@end

@implementation PKProductMainViewController


-(void) requestData
{
    
    NSMutableDictionary *parDic=[NSMutableDictionary dictionary];
    
    parDic [@"start"]=@"0";
    
    parDic[@"client"] = @"1";
    
    parDic[@"deviceid"]=@"63A94D37-33F9-40FF-9EBB-481182338873";
    
    parDic [@"limit"]=@"10";
    parDic[@"auth"]=@"";
    parDic[@"version"] = @"3.0.2";
    
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/pub/shop" parDic:parDic finish:^(NSData *data) {
        NSDictionary *DIC=[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSArray *listArray=DIC[@"data"][@"list"];
        [_dataSuorceArray removeAllObjects];
        for (NSDictionary *modelDic in listArray) {
            PKproductMainListModel *model=[[PKproductMainListModel alloc]init];
            
            [model setValuesForKeysWithDictionary:modelDic];
            
            [_dataSuorceArray addObject:model];
        }
        NSLog(@"____刷新成功");
        
        [_mainTableView reloadData];
        
        _isloading = NO;
        
    } error:^(NSError *error) {
        _isloading = NO;
        NSLog(@"良品主页面网络请求失败%@",error);
    }];


}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentOffset.y);
    CGFloat scrollviewY=scrollView.contentOffset.y;
    //判断是否被拖拽
    if (scrollView.isDragging) {
        if (scrollviewY<0 && scrollviewY>-50) {
            _titlelabel.text = @"用力点就可以刷新了";
        }
        if (scrollviewY<-50) {
            _titlelabel.text = @"松开手就可以刷新了";
        }
        
    }else
    {
        if (scrollviewY<-50 &&_isloading==NO) {
            
            _isloading = YES;
            NSLog(@"开始刷新");
            _titlelabel.text = @"正在刷新";
            [self requestData];
        }
        
        if (_isloading) {
            if (scrollviewY >=-50) {
                scrollView.contentOffset=CGPointMake(0, -50);
            }
        }
    
    }
    
    
    
//    if (scrollView.contentOffset.y<-50&&_isloading ==NO) {
//        NSLog(@"刷新");
//        
//        _isloading = YES;
//        
//        [self requestData];
//    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLable.text= @"良品";
    
    _isloading = NO;
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70, WIHES,HHIHEI-70 )];
    
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = 300;
    [self.view addSubview:_mainTableView];
    
    _dataSuorceArray = [NSMutableArray array];
    
    _titlelabel =[[UILabel alloc]initWithFrame:CGRectMake(0, -50, WIHES, 50)];
    
    _titlelabel.textAlignment= NSTextAlignmentCenter;
    
    [_mainTableView addSubview:_titlelabel];
    
    [_mainTableView registerClass:[PKproductMainListTableViewCell class] forCellReuseIdentifier:@"pkprodu"];
    
    [self requestData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSuorceArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKproductMainListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"pkprodu" forIndexPath:indexPath];

        cell.configureModel = _dataSuorceArray[indexPath.row];
    [cell.buyButton addTarget:self action:@selector(handleBuyButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    cell.buyButton.tag = 1000+indexPath.row;

    return cell;
}

-(void)handleBuyButtonAction:(UIButton *)sender
{
//    PKproductMainListTableViewCell *cell=sender.superview.superview;
//    
//    NSIndexPath *indepath=[_mainTableView indexPathForCell:cell];
    
    
    PKproductMainListModel *model = _dataSuorceArray[sender.tag-1000];
    //跳转
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:model.buyurl]];

}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [PKproductMainListTableViewCell heightForcell];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
